package com.quanxiaoha.weblog.web.controller.record;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quanxiaoha.weblog.common.domain.entity.ScheduleSkill;
import com.quanxiaoha.weblog.common.domain.query.HabitCountQuery;
import com.quanxiaoha.weblog.common.domain.query.PaymentQuery;
import com.quanxiaoha.weblog.common.domain.query.ScheduleSkillQuery;
import com.quanxiaoha.weblog.common.domain.query.SkillQuery;
import com.quanxiaoha.weblog.common.domain.vo.CensusListVo;
import com.quanxiaoha.weblog.common.domain.vo.CensusVo;
import com.quanxiaoha.weblog.common.domain.vo.CountVo;
import com.quanxiaoha.weblog.common.domain.vo.ScheduleSkillVo;
import com.quanxiaoha.weblog.common.service.ISchedulePaymentService;
import com.quanxiaoha.weblog.common.service.IScheduleSkillService;
import com.quanxiaoha.weblog.common.service.impl.DeepSeekService;
import com.quanxiaoha.weblog.common.utils.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  技能管理控制器
 * </p>
 *
 * @author Roy
 * @since 2025-05-28
 */
@RestController
@Slf4j
@Api(tags = "技能管理", description = "技能的增删改查接口")
@RequestMapping("/todo")
public class ScheduleSkillController {

    @Autowired
    private IScheduleSkillService scheduleSkillService;

    @Autowired
    private ISchedulePaymentService schedulePaymentService;

    @Autowired
    private DeepSeekService deepSeekService;

    @ApiOperation(value = "获取技能列表", notes = "分页获取技能列表，支持按名称、完成状态等条件筛选")
    @PostMapping("/skill/list")
    public Response<List<ScheduleSkillVo>> getSkillList(@RequestBody SkillQuery skillQuery) {
        List<ScheduleSkillVo> skillList = scheduleSkillService.getSkillList(skillQuery);
        return Response.success(skillList);
    }

    @ApiOperation(value = "获取技能详情", notes = "根据ID获取技能的详细信息")
    @GetMapping("/skill/{id}")
    public ScheduleSkill getSkillDetail(@PathVariable Integer id) {
        return scheduleSkillService.getSkillDetail(id);
    }

    @ApiOperation(value = "新增技能", notes = "创建一个新的技能")
    @PostMapping("/skill")
    public void createSkill(@RequestBody ScheduleSkillQuery skill) {
        scheduleSkillService.createSkill(skill);
    }

    @ApiOperation(value = "更新技能", notes = "更新已存在的技能信息")
    @PutMapping("/skill")
    public void updateSkill(@RequestBody ScheduleSkill skill) {
        scheduleSkillService.updateSkill(skill);
    }

    @ApiOperation(value = "填写产出", notes = "填写产出")
    @PostMapping("/produce")
    public Response produce(@RequestBody ScheduleSkill skill) {
        scheduleSkillService.produce(skill);
        return Response.success();
    }

    @ApiOperation(value = "删除技能", notes = "根据ID删除指定的技能")
    @DeleteMapping("/skill/{id}")
    public void deleteSkill(@PathVariable Integer id) {
        scheduleSkillService.deleteSkill(id);
    }

    @ApiOperation(value = "完成阶段", notes = "打勾")
    @GetMapping("/skill/checkStage")
    public Response<List<ScheduleSkillVo>> getSkillList(Integer stageId) {
        scheduleSkillService.checkStage(stageId);
        return Response.success();
    }

    @ApiOperation(value = "任务列表", notes = "任务列表")
    @GetMapping("/skill/getTask")
    public Response<List<CountVo>> getTask() {
        List<CountVo> list = scheduleSkillService.getCountVos();
        return Response.success(list);
    }


    @ApiOperation(value = "保存分期", notes = "保存分期")
    @PostMapping("/savePayment")
    public Response savePayment(@RequestBody PaymentQuery query) {
        schedulePaymentService.savePayment(query);
        return Response.success();
    }

    @ApiOperation(value = "生成思维导图", notes = "生成思维导图")
    @PostMapping("/generate")
    public Response<String> generateMindMap(@RequestParam String topic) {
        try {
            String jsonData = deepSeekService.askDeepSeek(topic);
            return Response.success(jsonData); // 返回可直接渲染的JSON
        } catch (Exception e) {
            return Response.fail("生成失败");
        }
    }

}
